﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CM.BL.BE;
using CM.BL.BC;

namespace WebApp_2
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblErrorMsg.InnerHtml = string.Empty;
            if (!Page.IsPostBack)
            {
                string retVal = HelperBC.logOut(Session.SessionID);
                if (!string.IsNullOrEmpty(retVal)) { lblErrorMsg.InnerHtml = retVal; }
                Session.RemoveAll();
                Session.Clear();
                Session.Abandon();
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string retval = string.Empty; RfcConfigParametersBE objConn = new RfcConfigParametersBE();
            try
            {
                string strUname, strPswd;
                strUname = txtUname.Value.Trim(); strPswd = txtPasswd.Value.Trim();
                if (!string.IsNullOrEmpty(strUname) && !string.IsNullOrEmpty(strPswd))
                {
                    //-- Formar cadena de conexión:
                    //objConn = new RfcConfigParametersBE();
                    objConn.Id = Session.SessionID;
                    objConn.Passwd = strPswd;
                    objConn.Uname = strUname;

                    retval = HelperBC.getConnectionId(objConn);
                }
                else
                {
                    retval = "Debe ingresar todos los campos requeridos.";
                }
            }
            catch (Exception ex)
            {
                retval = ex.Message;
            }
            finally
            {
                retval = retval.IndexOf("TIMEDOUT") >= 0 ? "No se pudo establecer la conexión con el servidor." : retval;
                lblErrorMsg.InnerHtml = retval.Trim();
            }

            if (!string.IsNullOrEmpty(retval))
            {
                Session.RemoveAll();
                Session.Clear();
                Session.Abandon();
            }
            else
            {
                //logeado con éxito:
                Session["objConn"] = objConn;
                Response.Redirect("List.aspx");
            }
        }
    }
}
